package cn.tedu.buycradserver.service.impl;

import cn.tedu.buycradserver.mapper.AlbumMapper;
import cn.tedu.buycradserver.pojo.dto.AlbumAddNew;
import cn.tedu.buycradserver.pojo.entity.Album;
import cn.tedu.buycradserver.service.IAlbumService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.io.File;

@Service
public class AlbumServiceImpl implements IAlbumService {

    @Autowired
    private AlbumMapper albumMapper;

    //读取application.properties文件中的内容并赋值给变量
    @Value("${dirpath}")
    private String dirPath;

    @Override
    public void addNew(AlbumAddNew albumAddNew) {
        Album album = new Album();
        BeanUtils.copyProperties(albumAddNew,album);
        albumMapper.insert(album);

    }

    @Override
    public void deleteById(Long id) {
        //通过商品的id查询出商品图片的路径
        String url = albumMapper.selectPathById(id);
        //得到完整的文件路径
        String filePath = dirPath+"/"+url;
        new File(filePath).delete();
        albumMapper.deleteById(id);
    }

    @Override
    public void updateById(Long id) {
        Album album = albumMapper.selectById(id);
        if(album != null){
            albumMapper.updateById(id);
        }
    }

    @Override
    public Album selectById(Long id) {
        Album album = albumMapper.selectById(id);
        return album;
    }
}
